package com.officegame.db.implementation.hibernate;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;

import com.officegame.db.dao.ITeamDAO;
import com.officegame.db.entities.Team;

public class TeamDAO implements ITeamDAO {

	@Override
	public void persist(Team team) {
		Session session = HibernateUtil.getSession();
		session.beginTransaction();
		session.saveOrUpdate(team);
		session.getTransaction().commit();
	}
	
	@Override
	public void create(Team team) {
		Session session = HibernateUtil.getSession();
		session.beginTransaction();
		session.save(team);
		session.getTransaction().commit();
	}

	@Override
	public Team read(Long id) {
		Session session = HibernateUtil.getSession();
		session.beginTransaction();
		Team player = (Team) session.createCriteria(Team.class)
			.add(Restrictions.eq("id", id)).uniqueResult();
		session.getTransaction().commit();
		return player;
	}

	@Override
	public void delete(Long id) {
		Session session = HibernateUtil.getSession();
		session.beginTransaction();
		Team teamToDelete = (Team) session.createCriteria(Team.class)
			.add(Restrictions.eq("id", id)).uniqueResult();
		session.delete(teamToDelete);
		session.getTransaction().commit();
	}

	@Override
	public List<Team> getTeams() {
		Session session = HibernateUtil.getSession();
		List<Team> teams = (List<Team>) session.createCriteria(Team.class).list();
		return teams;
	}

}
